"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 备节点执行restore过程中进行backup,restore -h指定主节点，backup -h指定备节点
Create At   : 2020/12/01
@zou_jialiang0505328126
Description :
    1.备节点备份
    2.备节点执行恢复-h指向备节点
    3.备节点恢复过程中备份 -h指向主节点
    4.检查集群状态
Expect      :
    1.备份成功
    2.备节点恢复成功
    3.备份成功
    4.查询集群状态正常
History     :
"""

import os
import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class Basebackup(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('--Opengauss_Reliability_Tools_Backup_Case022 start--')
        self.db_primary_db_user = Node(node='PrimaryDbUser')
        self.db_standby1_db_user = Node(node='Standby1DbUser')
        self.constant = Constant()
        self.common = Common('PrimaryRoot')
        self.backup_file = os.path.join(macro.DB_BACKUP_PATH, 'backup_dir')
        self.primary_hostname = self.db_primary_db_user.sh(
            'hostname').result()
        self.standby_hostname = self.db_standby1_db_user.sh(
            'hostname').result()

        self.log.info('------------创建备份路径----------------')
        shell_cmd = f'rm -rf {self.backup_file}; mkdir {self.backup_file};'
        self.log.info(shell_cmd)
        result = self.db_standby1_db_user.sh(shell_cmd).result()
        self.log.info(result)

    def test_backup(self):
        self.log.info("---------------1.备节点备份：成功---------------------")
        result = self.commonshsta.execu_gs_backup(
            'backup', self.standby_hostname, self.backup_file, "--all")
        self.log.info(result)
        self.assertIn(self.constant.BACKUP_BACKUP_SUCCESS_MSG, result)
        cmd = f'ls -l {self.backup_file} |grep "^-"|wc -l'
        self.log.info(cmd)
        result = self.db_standby1_db_user.sh(cmd).result()
        self.log.info(result)
        self.assertLess(0, int(result))

        self.log.info('----2.备节点执行恢复-h指向备节点：成功--------')
        restore_thread = ComThread(
            self.commonshsta.execu_gs_backup,
            args=('restore',
                  self.standby_hostname, self.backup_file, "--all",))
        restore_thread.setDaemon(True)
        restore_thread.start()

        self.log.info('---------3.备节点恢复过程中备份 -h指向主节点:成功-------------')
        result = self.commonshsta.execu_gs_backup(
            'backup', self.primary_hostname, self.backup_file, "--all")
        self.log.info(result)
        self.assertIn(self.constant.BACKUP_BACKUP_SUCCESS_MSG, result)
        cmd = f'ls -l {self.backup_file} |grep "^-"|wc -l'
        self.log.info(cmd)
        result = self.db_primary_db_user.sh(cmd).result()
        self.log.info(result)
        self.assertLess(0, int(result))

        self.log.info("============检查步骤2结果====================")
        restore_thread.join(10 * 60)
        result = restore_thread.get_result()
        self.log.info(result)
        self.assertIn(self.constant.BACKUP_RESTORE_SUCCESS_MSG, result)

        self.log.info('-----------4.检查集群状态:成功----------')
        result = self.commonshpri.getDbClusterStatus("status")
        self.assertTrue(result)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('--------------------------删除备份文件------------------')
        shell_cmd = f'rm -rf {self.backup_file}'
        self.log.info(shell_cmd)
        result = self.db_primary_db_user.sh(shell_cmd).result()
        self.log.info(result)
        result = self.db_standby1_db_user.sh(shell_cmd).result()
        self.log.info(result)
        self.log.info('--Opengauss_Reliability_Tools_Backup_Case022.py end--')
